package com.jpxx.netty.util;

import static org.junit.Assert.assertEquals;  

import java.io.DataInputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
  

import org.junit.Test;  
  
/** 
 * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a> 
 * @since 1.0 
 */  
public class GZipUtilsTest {  
  
    private String inputStr = "zlex@zlex.org,snowolf@zlex.org,zlex.snowolf@zlex.org";  
    
    //压缩图片006.jpg
    @Test
    public final void yasuotupian() throws Exception {  
    	//读取文件
    	String readPath = System.getProperty("user.dir")+File.separatorChar+"sources"+File.separatorChar+"006.jpg";
    	System.out.println("readPath"+readPath);
    	File file = new File(readPath);
    	FileInputStream  in = new FileInputStream(file);
    	byte[] data = new byte[in.available()];
    	in.read(data);
    	in.close();
    	
    	System.out.println("原始文件大小："+data.length);
    	//测试压缩
    	byte[] yasuo = GZipUtilsOK.compress(data);
    	System.out.println("压缩后大小："+yasuo.length);
    	//解压缩
    	byte[] jieyasuo = GZipUtilsOK.decompress(yasuo);
    	System.out.println("解压缩大小："+jieyasuo.length);
    			
    	//写出文件
    	String writePath = System.getProperty("user.dir")+File.separatorChar+"receive"+File.separatorChar+"006.jpg";
    	System.out.println("writePath:"+writePath);
    	FileOutputStream fos = new FileOutputStream(writePath);
    	fos.write(jieyasuo);
    	fos.close();
    }
  
    
    
    
    //测试字符串
    @Test  
    public final void testDataCompress() throws Exception {  
  
        System.err.println("原文:\t" + inputStr);  
  
        byte[] input = inputStr.getBytes();  
        System.err.println("长度:\t" + input.length);  
  
        byte[] data = GZipUtilsOK.compress(input);  
        System.err.println("压缩后:\t"+data.toString());  
        System.err.println("长度:\t" + data.length);  
  
        byte[] output = GZipUtilsOK.decompress(data);  
        String outputStr = new String(output);  
        System.err.println("解压缩后:" + outputStr);  
        System.err.println("长度:\t" + output.length);  
  
        assertEquals(inputStr, outputStr);  
  
    }  
  
    //文件的压缩和解压缩
    @Test  
    public final void testFileCompress() throws Exception {  
  
        FileOutputStream fos = new FileOutputStream("d:/f.txt");  
  
        fos.write(inputStr.getBytes());  
        fos.flush();  
        fos.close();  
        //文件压缩 
        GZipUtilsOK.compress("d:/f.txt", false);  
        //文件解压缩
        GZipUtilsOK.decompress("d:/f.txt.gz", false);  
  
        File file = new File("d:/f.txt");  
  
        FileInputStream fis = new FileInputStream(file);  
  
        DataInputStream dis = new DataInputStream(fis);  
  
        byte[] data = new byte[(int) file.length()];  
        dis.readFully(data);  
  
        fis.close();  
  
        String outputStr = new String(data);  
        assertEquals(inputStr, outputStr);  
    }  
}  